﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Reflection;
using Xin478.CSWork.Common;
using Xin478.MySocket;

namespace Xin478.CSWork.Client
{
    public partial class AddRemindForm : MyForm
    {
        Regex regEmpty = new Regex("^[　| |\\s]+$");
        Regex regInt = new Regex("^\\d+$");
        string title = "添加提醒";

        public AddRemindForm()
        {
            InitializeComponent();
            this.Text = title;
            this.Load+=new EventHandler(AddRemindForm_Load);
            this.Closed += new EventHandler(AddRemindForm_Closed);
        }

        void AddRemindForm_Closed(object sender, EventArgs e)
        {
            ((MainForm)this.Owner).CommandReceived -= new CommandReceiveHandler(CommandReceive);
        }

        void CommandReceive(DatagramBuilder builder, ref bool cancel)
        {
            MethodInfo mi = typeof(AddRemindForm).GetMethod(builder.Name, BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if (mi != null)
            {
                mi.Invoke(this, builder.Params);
                cancel = true;
            }
        }

        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb=(RadioButton)sender;
            if (rb.Checked)
            {
                if (checkBox3.Checked) this.dateTimePicker1.CustomFormat = rb.Tag.ToString().Substring(0, rb.Tag.ToString().IndexOf(' '));
                else this.dateTimePicker1.CustomFormat = rb.Tag.ToString();
            }
        }

        void AddRemindForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
            this.dateTimePicker1.MinDate = DateTime.Now;
            radioButton1.Tag = "yyyy年MM月dd日 HH:mm";
            radioButton2.Tag = "每天 HH:mm";//00-00 01:23
            radioButton3.Tag = "每周ddd HH:mm";//1 01:23
            radioButton4.Tag = "每月dd日 HH:mm";
            this.dateTimePicker1.CustomFormat = radioButton1.Tag.ToString();
            this.radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            this.radioButton3.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            this.radioButton4.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            this.checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);
            this.textBox3.TextChanged += new EventHandler(textBox3_TextChanged);
            this.checkBox2.CheckedChanged += new EventHandler(checkBox2_CheckedChanged);
            this.textBox1.TextChanged += new EventHandler(textBox_TextChanged);
            this.textBox2.TextChanged += new EventHandler(textBox_TextChanged);
            this.checkBox3.CheckedChanged += new EventHandler(checkBox3_CheckedChanged);
        }

        void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox2.Enabled = false;
                textBox2.Enabled = false;
                this.dateTimePicker1.CustomFormat = this.dateTimePicker1.CustomFormat.Substring(0, this.dateTimePicker1.CustomFormat.IndexOf(' '));
            }
            else
            {
                checkBox2.Enabled = true;
                textBox2.Enabled = true;
                this.dateTimePicker1.CustomFormat += " HH:mm";
            }
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb=(TextBox)sender;
            int len=tb.Text.Length;

            if(len>0 && !regInt.IsMatch( tb.Text))
            {
                tb.Text = len==1 ? "3" : tb.Text.Substring(0, len - 1);
            }
        }

        void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.Enabled=checkBox2.Checked;
        }

        void textBox3_TextChanged(object sender, EventArgs e)
        {
            button2.Enabled = !(string.IsNullOrEmpty(textBox3.Text) || textBox3.Text.Length == 0 || regEmpty.IsMatch(textBox3.Text));
        }

        void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
            else
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RemindInfo ri = new RemindInfo();
            ri.UserId = UserSession.Current.EmployeeInfo.Id;
            ri.Detail = textBox3.Text;
            

            DateTime dt = dateTimePicker1.Value;
            
            ri.DefiniteTime = checkBox3.Checked? -1: dt.Hour*100+dt.Minute;

            if (checkBox1.Checked) ri.Interval = Convert.ToInt32(textBox1.Text);
            if (checkBox2.Checked && !checkBox3.Checked) ri.DelayTime = Convert.ToInt32(textBox1.Text);
            else ri.DelayTime = 0;

            if (radioButton1.Checked)
            {
                ri.DefiniteDate = dt.ToString("yyyy-MM-dd");
            }
            else if (radioButton2.Checked)
            {
                ri.PlanType = 0;
            }
            else if (radioButton3.Checked)
            {
                ri.PlanType = -Convert.ToInt32(Enum.Parse(typeof(DayOfWeek), dt.DayOfWeek.ToString()));
            }
            else if (radioButton4.Checked)
            {
                ri.PlanType = dt.Day;
            }

            MainForm mf=((MainForm)this.Owner);
            mf.CommandReceived += new CommandReceiveHandler(CommandReceive);
            mf.Send("Remind.添加", ri);
            this.button2.Enabled = false;
            this.button1.Enabled = false;
            this.Text = "正在提交......";
        }

        void 提醒添加(NetEventArgs e, bool isSuccess)
        {
            WinForm.DoThreadSafe(this, delegate()
            {
                if (isSuccess)
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败");
                    this.Text = title;
                    this.button2.Enabled = true;
                    this.button1.Enabled = true;
                }
            });
        }
    }
}
